<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h3>获取根组件实例</h3>
      <child></child>
    </div>

    <script src="../Vue.js"></script>
    <script>
      const Child2 = {
        template: `
          <div>
            child2
          </div>
        `,
        mounted() {
          console.log(this.$root.count);
          this.$root.fn();
        },
      };

      const Child = {
        template: `
          <div>
            child
            <child2></child2>
          </div>
        `,
        components: {
          Child2,
        },
        mounted() {
          this.$parent.fn();
        },
      };

      new Vue({
        el: "#app",
        data: {
          count: 10,
        },
        methods: {
          fn() {
            console.log("fn");
          },
        },
        components: {
          Child,
        },
      });
    </script>
  </body>
</html>
